
const db = require('db');

module.exports = async ctx => {
    const { connection, run } = db.init();
    try {
        ctx.status = 200;
        ctx.body = {
            ...await getBasics(ctx, run),
            invoicingMessage: await getInvoicingMessage(ctx, run),
        };
    } catch (error) {
        ctx.status = 500;
        console.log(error);
        ctx.body = error;
    }
    connection.end();
}


/** 获取基础数据 */
async function getBasics(ctx, run) {
    const id = ctx.params.id;
    const sqlStr = `
        SELECT * 
        FROM client 
        WHERE id=${id}
    `;

    const result = await run(sqlStr);
    if (result.err) {
        throw result.err
    }

    return result.rows[0];
}

/** 获取发票数据 */
async function getInvoicingMessage(ctx, run) {
    const id = ctx.params.id;
    const sqlStr = `
        SELECT *
        FROM clientInvoicingMessage
        WHERE clientId = ${id}
    `;

    const result = await run(sqlStr);
    if (result.err) {
        throw result.err
    }

    return result.rows
}